package com.project.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.project.bean.ClassBean;
import com.project.bean.FacilitiesBean;
import com.project.bean.RoomBean;
import com.project.mapper.IFacilitiesMapper;
import com.project.mapper.IRoomMapper;
import com.project.service.IClassService;
import com.project.service.IFacilitiesService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.lang.ref.PhantomReference;
import java.util.List;

@Service
@Transactional
public class FacilitiesServiceImpl implements IFacilitiesService {

    @Autowired
    private IRoomMapper roomMapper;

    @Autowired
    private IFacilitiesMapper facMapper;


    @Override
    public List<FacilitiesBean> findByRoomId(Integer roomId) {
        QueryWrapper<FacilitiesBean> qw = new QueryWrapper<>();
        qw.eq("fk_roomId",roomId);


        return facMapper.selectList(qw);
    }

    @Override
    public void add(FacilitiesBean facilities) {
        facMapper.insert(facilities);
        RoomBean roomBean = roomMapper.selectById(facilities.getRoomId());
        roomBean.setStatus("设施损坏");
        roomMapper.updateById(roomBean);
    }

    @Override
    public void update(Integer facilityId, Integer roomId) {
        FacilitiesBean facilitiesBean = facMapper.selectById(facilityId);
        facilitiesBean.setStatus("已解决");
        facMapper.updateById(facilitiesBean);

        QueryWrapper<FacilitiesBean> qw = new QueryWrapper<>();
        qw.eq("s_status","未解决");
        qw.eq("fk_roomId",roomId);

        Integer num =facMapper.selectCount(qw);
        if (num == 0){
            RoomBean room =roomMapper.selectById(roomId);
            room.setStatus("正常");
            roomMapper.updateById(room);
        }
    }
}
